//
//  EMSSkillFieldCell.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 2/4/09.
//  Copyright 2009 __Vadim__. All rights reserved.
//

#import "EMSLearnedSkillFieldCell.h"
#import "EVEAPICoreDataController.h"
#import "EMSGeneralUtils.h"
#import "EMSSkillTrainingInfo.h"
#import "EVECDLearnedGroup.h"
#import "EVECDLearnedSkill.h"
#import "EVECDSkillGroup.h"
#import "EVECDSkill.h"

@implementation EMSLearnedSkillFieldCell 

@synthesize learnedGroup;
@synthesize learnedSkill;

-(NSColor*)stringColor
{
	return [self isHighlighted] ? [NSColor alternateSelectedControlTextColor] : [NSColor textColor];
}

-(NSDictionary*)boldStringAttributesWithColor:(NSColor*)stringColor
{
	return [NSDictionary dictionaryWithObjectsAndKeys:stringColor, NSForegroundColorAttributeName,
			[NSFont boldSystemFontOfSize:10], NSFontAttributeName, nil];
}

-(NSDictionary*)regularStringAttributesWithColor:(NSColor*)stringColor
{
	return [NSDictionary dictionaryWithObjectsAndKeys:stringColor, NSForegroundColorAttributeName,
			[NSFont systemFontOfSize:10], NSFontAttributeName, nil];
}

#pragma mark Print methods

-(void)drawRegularString:(NSString*)aName atPoint:(NSPoint)point
{
	[aName drawAtPoint:point withAttributes:[self regularStringAttributesWithColor:[self stringColor]]];
}

-(void)drawBoldString:(NSString*)aName atPoint:(NSPoint)point
{
	[aName drawAtPoint:point withAttributes:[self boldStringAttributesWithColor:[self stringColor]]];
}

#pragma mark Lots of the coordinate numbers need clean up

-(NSString*)formattedSPStringForPoints:(int)currPoints level:(int)aLevel rank:(int)aRank
{
	int skillPointsPerLevelForRank1[6] = {0, 250, 1414, 8000, 45255, 256000};
	NSNumberFormatter* numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
	[numberFormatter setFormat:@"###,###,###,###"];
	
	if(aLevel == 5)
	{
		int totalPoints = skillPointsPerLevelForRank1[aLevel] * aRank;
		return [NSString stringWithFormat:@"SP: %@/%@", 
				[numberFormatter stringFromNumber:[NSNumber numberWithInt:totalPoints]], 
				[numberFormatter stringFromNumber:[NSNumber numberWithInt:totalPoints]]];
	} else {
		int maxPointsForLevel = skillPointsPerLevelForRank1[aLevel+1] * aRank;
		return [NSString stringWithFormat:@"SP: %@/%@", 
				[numberFormatter stringFromNumber:[NSNumber numberWithInt:currPoints]],
				[numberFormatter stringFromNumber:[NSNumber numberWithInt:maxPointsForLevel]]];
	}
}

-(void)drawName
{
	if(self.learnedGroup)
	{
		[self drawBoldString:self.learnedGroup.group.name atPoint:NSMakePoint(leftSide, top)];
	} else {
		[self drawBoldString:self.learnedSkill.skill.name atPoint:NSMakePoint(leftSide, top)];
	}
}

-(void)drawRank
{
	
	NSSize skillNameStringSize = [self.learnedSkill.skill.name sizeWithAttributes:[self boldStringAttributesWithColor:[self stringColor]]];
	[self drawRegularString:[NSString stringWithFormat:@"(Rank %i)", rank] 
					atPoint:NSMakePoint(leftSide + skillNameStringSize.width + 3, top)];
}

-(void)drawSkillPointsString
{
	if(self.learnedSkill)
	{
		NSSize skillNameStringSize = [self.learnedSkill.skill.name sizeWithAttributes:
									  [self boldStringAttributesWithColor:[self stringColor]]];
		NSString* skillPointsString = [self formattedSPStringForPoints:skillPointsLearned
																 level:level
																  rank:rank];
		[self drawRegularString:skillPointsString atPoint:NSMakePoint(leftSide, top + skillNameStringSize.height)];	
	}
	if(self.learnedGroup)
	{
		NSNumberFormatter* numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
		[numberFormatter setFormat:@"###,###,###,###"];
		NSSize groupNameStringSize = [self.learnedGroup.group.name sizeWithAttributes:
									  [self boldStringAttributesWithColor:[self stringColor]]];
		int learnedSkillsCount = [self.learnedGroup.learnedSkills count];
		int trainedSkillsCount = [[self.learnedGroup valueForKey:@"trainedSkills"] count];
		NSNumber* trainedPoints = self.learnedGroup.skillPointsLearned;
		NSString* skillPointsString = [NSString stringWithFormat:@"(%i of %i skills) %@ points", 
									   trainedSkillsCount, learnedSkillsCount, [numberFormatter stringFromNumber:trainedPoints]];
		[self drawRegularString:skillPointsString atPoint:NSMakePoint(leftSide + groupNameStringSize.width + 3, top)];
	}
}

-(void)drawLevel
{
	NSString* currentLevelString = [NSString stringWithFormat:@"Level %i", level];
	NSSize currentLevelStringSize = [currentLevelString sizeWithAttributes:[self regularStringAttributesWithColor:[self stringColor]]];
	[self drawRegularString:currentLevelString atPoint:NSMakePoint(rightSide - currentLevelStringSize.width - 70, top)];
}

-(void)drawPercentCompleted
{
	NSSize skillNameStringSize = [self.learnedSkill.skill.name sizeWithAttributes:
								  [self boldStringAttributesWithColor:[self stringColor]]];
	NSString* percentCompletedString = [NSString stringWithFormat:@"%i%% Done", percentCompleted];
	NSSize percentCompletedStringSize = [percentCompletedString sizeWithAttributes:
										 [self regularStringAttributesWithColor:[self stringColor]]];
	[self drawRegularString:percentCompletedString 
					atPoint:NSMakePoint(rightSide - percentCompletedStringSize.width - 70, top + skillNameStringSize.height)];
}

-(void)drawBars
{
	//Add a new draw layer
	[[NSGraphicsContext currentContext] saveGraphicsState];
	[[NSGraphicsContext currentContext] setShouldAntialias:NO];
	NSImageInterpolation interpolation = [[NSGraphicsContext currentContext] imageInterpolation];
	[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
	[[NSColor blackColor] set];
	
	//Draw Level Status Bar	
	[NSBezierPath strokeRect:NSMakeRect(rightSide - 66, top + 2, 62, 13)];
	
	//Draw filled in boxes for current level as well as gray boxes
	int i;
	for(i = 0; i < 5; i++)
	{
		if(i >= level)
		{
			[[NSColor lightGrayColor] set];
		}
		[NSBezierPath fillRect:NSMakeRect(rightSide - 64 + (i * 12), top + 3, 11, 10)];
	}
	[[NSColor blackColor] set];
	
	//Draw Percent Done Status Bar
	[NSBezierPath strokeRect:NSMakeRect(rightSide - 66, top + 18, 62, 9)];
	[[NSColor lightGrayColor] set];
	[NSBezierPath setDefaultLineWidth:5.0];
	[NSBezierPath strokeLineFromPoint:NSMakePoint(rightSide - 64, top + 22) 
							  toPoint:NSMakePoint(rightSide - 5 , top + 22)];
	if(percentCompleted)
	{
		[[NSColor blackColor] set];
		float percentBarEnd = 59 - (59 * percentCompleted) / 100;
		[NSBezierPath strokeLineFromPoint:NSMakePoint(rightSide - 64, top + 22) 
								  toPoint:NSMakePoint(rightSide - percentBarEnd - 5 , top + 22)];
	}
	
	//Finalize new draw layer
	[[NSGraphicsContext currentContext] setImageInterpolation: interpolation];
	[[NSGraphicsContext currentContext] restoreGraphicsState];	
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{	
	
	//set some defaults
	[NSBezierPath setDefaultLineWidth:1.0];
	[NSBezierPath setDefaultLineJoinStyle:NSBevelLineJoinStyle];
	leftSide = cellFrame.origin.x;
	rightSide = cellFrame.origin.x + cellFrame.size.width;
	top = cellFrame.origin.y;
	
	NSManagedObjectID* data = [self objectValue];
	self.learnedGroup = nil;
	self.learnedSkill = nil;
	
	if([[[[EVECDContext objectWithID:data] entity] name] isEqualToString:@"EVECDLearnedGroup"])
	{
		self.learnedGroup = (EVECDLearnedGroup*)[EVECDContext objectWithID:data];
		[self drawName];
		[self drawSkillPointsString];
		
	} else {
		self.learnedSkill = (EVECDLearnedSkill*)[EVECDContext objectWithID:data];
		level = learnedSkill.level.intValue;
		rank = learnedSkill.skill.rank.intValue;
		skillPointsLearned = learnedSkill.skillPointsLearned.intValue;
		percentCompleted = [EMSSkillTrainingInfo percentCompletedForCurrentPoints:skillPointsLearned
																			level:level
																			 rank:rank];
		[self drawName];
		[self drawRank];
		[self drawSkillPointsString];
		[self drawLevel];
		[self drawPercentCompleted];
		[self drawBars];
	}
}

-(NSString*)description
{
	return [NSString stringWithFormat:@"%@ value: %@", [super description], [self objectValue]];
}

-(NSRect)expansionFrameWithFrame:(NSRect)cellFrame inView:(NSView*)view
{
	return NSZeroRect;
}

@end
